package com.springboot.chapter4.aspect.controller;

import com.springboot.chapter4.aspect.service.UserService;
import com.springboot.chapter4.aspect.pojo.User;
import com.springboot.chapter4.aspect.service.impl.UserServiceImpl;
import com.springboot.chapter4.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 作者：paopao
 * 版本：1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService = null;

//    @Autowired
//    private UserServiceImpl userService = null;

    @RequestMapping("/print")
    // 转换为JSON
    @ResponseBody
    public User printUser(Long id, String name, String note){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setNote(note);
        userService.printUser(user);
        return user;
    }

    @RequestMapping("/vp")
    @ResponseBody
    public User validateAndPrint(Long id, String name, String note){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setNote(note);
        // 强制转换
        UserValidator userValidator = (UserValidator) userService;
        // 验证用户是否为空
        if(userValidator.validate(user)){
            userService.printUser(user);
        }
        return user;
    }

    @RequestMapping("/manyAspects")
    public String manyAspects(){
        userService.manyAspects();
        return "manyAspects";
    }
}
